﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Submarines
{
    class Player : Board
    {
        Button _Source;

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Player
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.Name = "Player";
            this.Size = new System.Drawing.Size(469, 467);
            this.ResumeLayout(false);

        }

        void UpdateDetails(Button source)
        {
            string type = source.Name.Substring(0,1);
            string size = source.Name.Substring(1,1);
            Setup setup = source.Parent as Setup;

            int Amount = int.Parse((string)setup.Controls[type + size].Tag) - 1;
            setup.Controls[type + size].Tag = Amount.ToString();
            if (Amount == 0) setup.Controls[type + size].Enabled = false;
            
            if (type == "H") type = "V"; else type = "H";

            Amount = int.Parse((string)setup.Controls[type + size].Tag) - 1;
            setup.Controls[type + size].Tag = Amount.ToString();
            if (Amount == 0) setup.Controls[type + size].Enabled = false;
        }

        protected override void OnDragDrop(object sender, DragEventArgs e)
        {
            Button target = (Button)sender;
            Button source = (Button)e.Data.GetData(typeof(Button));

            int size = GetSize(source.Name);
            bool vertical = GetShape(source.Name);
            int x = GetX(target.Name);
            int y = GetY(target.Name);

            if (SubmarineValid(x, y, size, vertical))
            {
                e.Effect = DragDropEffects.Link;
                DrawSubmarine(x, y, size, vertical, _ExistColor);
                UpdateSubmarine(x, y, size, vertical, _Exists);
                InstallSubmarine(size);
                UpdateDetails(source);
            }
        }

        protected override void OnDragLeave(object sender, EventArgs e)
        {
            Button target = (Button)sender;
            Button source = _Source;

            int size = GetSize(source.Name);
            bool vertical = GetShape(source.Name);
            int x = GetX(target.Name);
            int y = GetY(target.Name);

            if (SubmarineValid(x, y, size, vertical))
            {
                DrawSubmarine(x, y, size, vertical, _OldBackColor);
                UpdateSubmarine(x, y, size, vertical, _Normal);
            }
        }

        protected override void OnDragEnter(object sender, DragEventArgs e)
        {
            Button target = (Button)sender;
            Button source = (Button)e.Data.GetData(typeof(Button));

            _Source = source;
            _OldBackColor = target.BackColor;

            int size = GetSize(source.Name);
            bool vertical = GetShape(source.Name);
            int x = GetX(target.Name);
            int y = GetY(target.Name);

            if (SubmarineValid(x, y, size, vertical))
            {
                e.Effect = DragDropEffects.Link;
                DrawSubmarine(x, y, size, vertical, _ShadowColor);
            }
            else e.Effect = DragDropEffects.None;
        }
    }
}
